package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.buildtable.pojo.Role;
import com.example.buildtable.pojo.User;
import com.example.buildtable.pojo.UserRole;
import com.example.common.util.R;
import com.example.buildtable.service.UserRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@Tag(name = "管理员角色")
@RequestMapping(value = "/userRole/")
public class UserRoleController {

    @Autowired
    private UserRoleService userRoleService;

    @Operation(summary = "查询所有管理员角色数据")
    @GetMapping(value = "/getAllUserRole")
    public R getAllUserRole() {
        LambdaJoinQueryWrapper<UserRole> wrapper = new LambdaJoinQueryWrapper<>(UserRole.class);
        wrapper.innerJoin(User.class, User::getUserId, UserRole::getUserId).
                innerJoin(Role.class, Role::getRoleId, UserRole::getRoleId);
        List<UserRole> userRoleList = userRoleService.list(wrapper);
        return R.ok("查询所有管理员角色数据成功").data("userRoleList", userRoleList);
    }

    @Operation(summary = "查询所有管理员角色分页数据")
    @GetMapping(value = "/getUserRolePage/{page}/{limit}")
    public R getUserRolePage(@PathVariable("page") Long page, @PathVariable("limit") Long limit) {
        Page<UserRole> userRolePage = new Page<>(page, limit);
        LambdaJoinQueryWrapper<UserRole> wrapper = new LambdaJoinQueryWrapper<>(UserRole.class);
        wrapper.innerJoin(User.class, User::getUserId, UserRole::getUserId).
                innerJoin(Role.class, Role::getRoleId, UserRole::getRoleId);
        userRoleService.page(userRolePage, wrapper);
        //每页的数据集合
        List<UserRole> userRoleList = userRolePage.getRecords();
        //总的记录数
        Long total = userRolePage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", userRoleList);
        return R.ok("查询所有管理员角色分页数据成功").data("map", map);
    }

    @Operation(summary = "增加管理员角色")
    @PostMapping(value = "/addUserRole")
    public R addUserRole(@RequestBody UserRole userRole) {
        boolean flag = userRoleService.save(userRole);

        if (flag) {
            return R.ok("管理员角色增加成功");
        } else {
            return R.ok("管理员角色增加失败");
        }
    }

    @Operation(summary = "删除管理员角色")
    @GetMapping(value = "/deleteUserRole/{userRoleId}")
    public R deleteUserRole(@PathVariable("userRoleId") Integer userRoleId) {
        boolean flag = userRoleService.removeById(userRoleId);
        if (flag) {
            return R.ok("管理员角色删除成功");
        } else {
            return R.ok("管理员角色删除失败");
        }
    }

    @Operation(summary = "按照管理员编号查看")
    @GetMapping(value = "/getUserRoleById/{userRoleId}")
    public R getUserRoleById(@PathVariable("userRoleId") Integer userRoleId) {
        LambdaJoinQueryWrapper<UserRole> wrapper = new LambdaJoinQueryWrapper<>(UserRole.class);
        wrapper.innerJoin(User.class, User::getUserId, UserRole::getUserId).
                innerJoin(Role.class, Role::getRoleId, UserRole::getRoleId);
        wrapper.eq(UserRole::getUserRoleId, userRoleId);
        UserRole userRole = userRoleService.getOne(wrapper);
        if (userRole != null) {
            return R.ok("按照管理员编号查看成功").data("userRole", userRole);
        } else {
            return R.error("按照管理员编号查看失败");
        }
    }

    @Operation(summary = "修改管理员")
    @PostMapping(value = "/updateUserRole")
    public R updateUserRole(UserRole userRole) {
        boolean flag = userRoleService.updateById(userRole);
        if (flag) {
            return R.ok("管理员修改成功");
        } else {
            return R.ok("管理员修改失败");
        }
    }
}
